home *** CD-ROM | disk | FTP | other *** search
/ ASME's Mechanical Engine…ing Toolkit 1997 December / ASME's Mechanical Engineering Toolkit 1997 December.iso / c_lang / sc.zoo / crypt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-06-03  |  3.8 KB  |  182 lines

  1. /*
  2.  * Encryption utilites
  3.  * Bradley Williams    
  4.  * {allegra,ihnp4,uiucdcs,ctvax}!convex!williams
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <curses.h>
  9.  
  10. #if defined(BSD42) || defined(BSD43)
  11. #include <sys/file.h>
  12. #else
  13. #include <fcntl.h>
  14. #endif
  15.  
  16. #include "sc.h"
  17.  
  18. extern char curfile[];
  19. char        *strcpy();
  20. #ifdef SYSV3
  21. void exit();
  22. #endif
  23.  
  24. int         Crypt = 0;
  25.  
  26. #define DEFWIDTH 10
  27. #define DEFPREC   2
  28.  
  29. creadfile (save, eraseflg)
  30. char *save;
  31. {
  32.     register FILE *f;
  33.     int pipefd[2];
  34.     int fildes;
  35.     int pid;
  36.  
  37.     if (eraseflg && strcmp(save, curfile) && modcheck(" first")) return;
  38.  
  39.     fildes = open (save, O_RDONLY, 0);
  40.     if (fildes < 0)
  41.     {
  42.     error ("Can't read %s", save);
  43.     return;
  44.     }
  45.  
  46.     if (eraseflg) erasedb ();
  47.  
  48.     if (pipe(pipefd) < 0) {
  49.     error("Can't make pipe to child");
  50.     return;
  51.     }
  52.  
  53.     deraw();
  54.     if ((pid=fork()) == 0)              /* if child  */
  55.     {
  56.     (void) close (0);          /* close stdin */
  57.     (void) close (1);          /* close stdout */
  58.     (void) close (pipefd[0]);      /* close pipe input */
  59.     (void) dup (fildes);          /* standard in from file */
  60.     (void) dup (pipefd[1]);          /* connect to pipe */
  61.     (void) fprintf (stderr, " ");
  62.     (void) execl ("/bin/sh", "sh", "-c", "crypt", 0);
  63.     exit (-127);
  64.     }
  65.     else                  /* else parent */
  66.     {
  67.     (void) close (fildes);
  68.     (void) close (pipefd[1]);      /* close pipe output */
  69.     f = fdopen (pipefd[0], "r");
  70.     if (f == 0)
  71.     {
  72.         (void) kill (pid, -9);
  73.         error ("Can't fdopen file %s", save);
  74.         (void) close (pipefd[0]);
  75.         return;
  76.     }
  77.     }
  78.  
  79.     while (fgets(line,sizeof line,f)) {
  80.     linelim = 0;
  81.     if (line[0] != '#') (void) yyparse ();
  82.     }
  83.     (void) fclose (f);
  84.     (void) close (pipefd[0]);
  85.     while (pid != wait(&fildes)) /**/;
  86.     goraw();
  87.     linelim = -1;
  88.     modflg++;
  89.     if (eraseflg) {
  90.     (void) strcpy (curfile, save);
  91.     modflg = 0;
  92.     }
  93.     EvalAll();
  94. }
  95.  
  96. cwritefile (fname, r0, c0, rn, cn)
  97. char *fname;
  98. {
  99.     register FILE *f;
  100.     register struct ent **p;
  101.     register r, c;
  102.     int pipefd[2];
  103.     int fildes;
  104.     int pid;
  105.     char save[1024];
  106.  
  107.     if (*fname == 0) fname = &curfile[0];
  108.  
  109.     (void) strcpy(save,fname);
  110.  
  111.     fildes = open (save, O_WRONLY|O_CREAT, 0600);
  112.     if (fildes < 0)
  113.     {
  114.     error ("Can't create %s", save);
  115.     return(-1);
  116.     }
  117.  
  118.     if (pipe (pipefd) < 0) {
  119.     error ("Can't make pipe to child\n");
  120.     return(-1);
  121.     }
  122.  
  123.     deraw();
  124.     if ((pid=fork()) == 0)              /* if child  */
  125.     {
  126.     (void) close (0);              /* close stdin */
  127.     (void) close (1);              /* close stdout */
  128.     (void) close (pipefd[1]);          /* close pipe output */
  129.     (void) dup (pipefd[0]);              /* connect to pipe input */
  130.     (void) dup (fildes);              /* standard out to file */
  131.     (void) fprintf (stderr, " ");
  132.     (void) execl ("/bin/sh", "sh", "-c", "crypt", 0);
  133.     exit (-127);
  134.     }
  135.     else                  /* else parent */
  136.     {
  137.     (void) close (fildes);
  138.     (void) close (pipefd[0]);          /* close pipe input */
  139.     f = fdopen (pipefd[1], "w");
  140.     if (f == 0)
  141.     {
  142.         (void) kill (pid, -9);
  143.         error ("Can't fdopen file %s", save);
  144.         (void) close (pipefd[1]);
  145.         return(-1);
  146.     }
  147.     }
  148.  
  149.     (void) fprintf (f, "# This data file was generated by the Spreadsheet ");
  150.     (void) fprintf (f, "Calculator.\n");
  151.     (void) fprintf (f, "# You almost certainly shouldn't edit it.\n\n");
  152.     for (c=0; c<MAXCOLS; c++)
  153.     if (fwidth[c] != DEFWIDTH || precision[c] != DEFPREC)
  154.         (void)fprintf(f,"format %s %d %d\n",coltoa(c),fwidth[c],precision[c]);
  155.     write_range(f);
  156.     if (mdir)
  157.     (void) fprintf(f, "mdir \"%s\"\n", mdir);
  158.     for (r=r0; r<=rn; r++) {
  159.     p = &tbl[r][0];
  160.     for (c=c0; c<=cn; c++, p++)
  161.         if (*p) {
  162.         if ((*p)->label) {
  163.             edits(r, c);
  164.             (void) fprintf(f, "%s\n", line);
  165.         }
  166.         if ((*p)->flags&is_valid) {
  167.             editv(r, c);
  168.             (void) fprintf (f, "%s\n",line);
  169.         }
  170.         }
  171.     }
  172.     (void) fclose (f);
  173.     (void) close (pipefd[1]);
  174.     while (pid != wait(&fildes)) /**/;
  175.     (void) strcpy(curfile,save);
  176.  
  177.     modflg = 0;
  178.     error("File '%s' written.",curfile);
  179.     goraw();
  180.     return(0);
  181. }
  182.